<template>
  <el-dialog
    title="新規"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form size="small" :model="form" ref="form">
      <el-form-item
        label="名称"
        :label-width="formLabelWidth"
        :rules="[{ required: true, message: ' ' }]"
        prop="title"
      >
        <el-input
          v-model="form.title"
          autocomplete="off"
          placeholder="名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="都道府県" :label-width="formLabelWidth">
        <el-select v-model="form.address" placeholder="選択してください">
          <el-option
            v-for="item in cityList"
            :key="item.title"
            :label="item.title"
            :value="item.title"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="部署"
        :label-width="formLabelWidth"
        :rules="[{ required: true, message: ' ' }]"
        prop="top_title"
      >
        <el-input
          v-model="form.top_title"
          autocomplete="off"
          placeholder="部署"
        ></el-input>
      </el-form-item>
      <el-form-item label="並び順" :label-width="formLabelWidth">
        <el-input v-model="form.sorts" placeholder="並び順"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible">キャンセル</el-button>
        <el-button type="primary" @click="addClick()" :loading="loading"
          >確定</el-button
        >
      </div>
    </template>
  </el-dialog>
</template>

<script>
import deptApi from "@/api/dept.js";
export default {
  data() {
    return {
      form: {
        status: 0,
        sort: 100,
      },
      categoryList: [],
      sort: "100",
      radio: "1",
      /*左边长度*/
      formLabelWidth: "120px",
      /*是否显示*/
      dialogVisible: false,
      loading: false,
      cityList: [],
    };
  },
  props: ["open_add"],
  created() {
    this.dialogVisible = this.open_add;
    this.getCityList();
  },
  methods: {
    /*获取列表*/
    getCityList() {
      let self = this;
      let Params = {};
      deptApi
        .citys(Params, true)
        .then((data) => {
          self.loading = false;
          self.cityList = data.data;
        })
        .catch((error) => {
          self.loading = false;
        });
    },
    /*添加插件*/
    addClick() {
      let self = this;
      let params = this.form;
      self.$refs.form.validate((valid) => {
        if (valid) {
          self.loading = true;
          deptApi
            .adds(params, true)
            .then((res) => {
              if (res.code == 1) {
                self.loading = false;
                ElMessage({
                  message: "追加成功",
                  type: "success",
                });
                self.dialogFormVisible(true);
              } else {
                self.loading = false;
              }
            })
            .catch((error) => {});
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit("closeDialog", {
          type: "success",
          openDialog: false,
        });
      } else {
        this.$emit("closeDialog", {
          type: "error",
          openDialog: false,
        });
      }
    },
  },
};
</script>

<style></style>
